<?php 
$sql = "select * from meterial_base_categories order by sort_order asc";
$rows = $this->db->query($sql)->result_array();


?>
<select id="category_id" name="category_id">
 <?php  foreach($rows as $base){

?>

  <optgroup label="<?php echo $base["cat_base_name"]; ?>">
 <?php 

 $cats   =array();
 $type_condition = "";
 if($type_id)
 {
 	$type_condition = " and type_id =".$type_id;
 }
 
 if($in_stork)
 {
		$sql = "select * from inventory1_views where (total - allocation_total ) >0  and  cat_base_id=".$base["cat_base_id"]." $type_condition";
 	
 	
 	
 	$cats = $this->db->query($sql)->result_array();
 }
 else{
 	$this->db->where("cat_base_id",$base["cat_base_id"]);
 	$this->db->where("type_id",$type_id);
 	$cats = $this->db->get("material_categories")->result_array();
 }
 
 foreach($cats as $row){ ?>
    <option value="<?php echo  $row["category_id"]; ?>"><?php echo  $row["category_name"]; ?> <?php if(isset($row["total"])){ ?> [ คงเหลือ:<?php echo ($row["total"]-$row["allocation_total"]); ?>] <?php } ?></option>
  
<?php } ?>
  </optgroup>
  <?php } ?>  
</select> 
	